<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('年度一览表生成列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>户编号：</label>
                                <input type="text" name="fid"/>
                            </li>
                            <li>
                                <label>姓名：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>证件号码：</label>
                                <input type="text" name="idCard"/>
                            </li>
                            <li>
                                <label>年度：</label>
                                <input type="text" name="reportYear"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="checkItem()">
                    <i class="fa fa-download"></i> 批量导出
                </a>
                <a class="btn btn-warning" onclick="allExport()">
                    <i class="fa fa-download"></i> 导出当年
                </a>
                <a class="btn btn-info" onclick="$.table.importExcel()">
                    <i class="fa fa-upload"></i> 导入模板
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/table";
        //获取标签所对应级别的名称
        var natureVillageDatas = [[${@dict.getType('base_nature_village')}]];
        $(function() {
            const options = {
                url: prefix + "/list",
                modalName: "年度一览表生成",
                importUrl: prefix + "/importData",//用于指定处理上传数据的 URL
                importTemplateUrl: prefix + "/importTemplate",//用于指定下载模板文件的 URL
                rememberSelected: true,
                columns: [{
                    field: 'state',
                    checkbox: true
                },
                    {
                        field: 'id',
                        title: 'ID'
                    },
                    {
                        field: 'fid',
                        title: '户编号'
                    },
                    {
                        field: 'name',
                        title: '姓名'
                    },
                    {
                        field: 'idCard',
                        title: '证件号码'
                    },
                    {
                        field: 'natVillage',
                        title: '自然村',
                        //有一列使用了formatter方法，该方法格式化并返回特定的值，
                        // 该值必须首先从一个名为natureVillageDatas的字典表中获取到对应的标签名。
                        formatter: function (value, row, index) {
                            //根据类型与对应的编号，获取字典的值
                            return $.table.selectDictLabel(natureVillageDatas, value);
                        }
                    },
                    {
                        field: 'reportYear',
                        title: '年度'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function (value, row, index) {
                            const actions = [];
                            actions.push('<a class="btn btn-warning btn-xs ' + '" href="javascript:void(0)" onclick= singleExcel(\'' + row.id + '\')><i class="fa fa-download"></i>导出</a>');
                            return actions.join('');
                        }
                    }]
            };
            $.table.init(options);
            $.table.hideColumn('id');
        });
        function allExport() {
            $.ajax({
                url:prefix + "/exportAll",
                type: "GET",
                contentType:'application/json;charset=utf8',
                success:function (result) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                }
            })

        }
        function singleExcel(id){
            $.ajax({
                url:prefix + "/singleExcel",
                type:"GET",
                data:{"id":id},
                contentType:'application/json;charset=utf8',
                success:function (result) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                },
                complete:function (result){
                    if (result.statusText === 'error'){
                        $.modal.alertWarning(result.responseJSON.msg);
                        $.modal.enable();
                        $.modal.closeLoading();
                    }
                },
            })
        }
        // 选中数据
        function checkItem(){
            table.set();
            const rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length === 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.ajax({
                url:prefix + "/selectExport",
                type:"GET",
                data: {"ids":rows.join()},
                contentType:'application/json;charset=utf8',
                success:function (result) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                },
            })
        }
    </script>
     <!-- 导入区域 -->
     <script id="importTpl" type="text/template">
         <form enctype="multipart/form-data" class="mt20 mb10">
             <div class="col-xs-offset-1">
                 <input type="file" id="file" name="file"/>
                 <div class="mt10 pt5">
                     <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
                 </div>
                 <font color="red" class="pull-left mt10">
                     提示：仅允许导入“xls”或“xlsx”格式文件！
                 </font>
             </div>
         </form>
     </script>
</body>
</html>